package com.hjm.app.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.hjm.app.R;
import com.hjm.app.activity.OrderActivity;
import com.hjm.app.activity.UserActivity;
import com.hjm.java.entity.UserInfo;
import com.hjm.java.utils.HjmObjectCallBack;
import com.hjm.ui.common.Cell;
import com.hjm.ui.common.ImageLabel;

/**
 * 用户信息简介
 */
public class FragmentHomeUserIntro extends Fragment {
    private static final String TAG = "FragmentHomeUserIntro";
    private LinearLayout userInfoLayout;
    private TextView userNameTextView, phoneTextView;
    private ImageView userImageView;
    private Cell addressCell;
    private ImageLabel noPayImageLabel, noRXImageLabel, noTXImageLabel, doneImageLabel;
    private UserInfo userInfo;
    private Intent orderIntent;
    private View view;
    private OnUserInfoListener listener;

    public FragmentHomeUserIntro(OnUserInfoListener listener) {
        // Required empty public constructor
        this.listener = listener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderIntent = new Intent(getContext(), OrderActivity.class);
    }

    @Override
    public void onResume() {
        super.onResume();
        // TOOD:当存在token时从本地获取用户信息，否则显示需要登录
        listener.getUserInfo((isSuccess, userInfo, msg) -> {
            if (isSuccess) {
                this.userInfo = userInfo;
                setUserInfoOnView();
            } else {
                Log.d(TAG, "getUserInfo: 获取用户信息失败\n" + msg);
                hideUserInfoOnView();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_user_intro, container, false);
        userInfoLayout = view.findViewById(R.id.layout_home_user_info);

        userNameTextView = view.findViewById(R.id.text_view_user_name);
        phoneTextView = view.findViewById(R.id.text_view_user_phone);
        userImageView = view.findViewById(R.id.image_view_user_image);

        addressCell = view.findViewById(R.id.cell_user_address);
        addressCell.setOnclickListener((v) -> {
            orderIntent.putExtra(OrderActivity.TYPE, OrderActivity.TYPE_ADDRESS);
            startActivity(orderIntent);
        });
        noPayImageLabel = view.findViewById(R.id.label_user_order_no_pay);
        noPayImageLabel.setOnclickListener((v) -> {
            orderIntent.putExtra(OrderActivity.TYPE, OrderActivity.TYPE_ORDER_NO_PAY);
            startActivity(orderIntent);
        });
        noRXImageLabel = view.findViewById(R.id.label_user_order_no_rx);
        noRXImageLabel.setOnclickListener((v) -> {
            orderIntent.putExtra(OrderActivity.TYPE, OrderActivity.TYPE_ORDER_NO_RX);
            startActivity(orderIntent);
        });
        noTXImageLabel = view.findViewById(R.id.label_user_order_no_tx);
        noTXImageLabel.setOnclickListener((v) -> {
            orderIntent.putExtra(OrderActivity.TYPE, OrderActivity.TYPE_ORDER_NO_TX);
            startActivity(orderIntent);
        });
        doneImageLabel = view.findViewById(R.id.label_user_order_done);
        doneImageLabel.setOnclickListener((v) -> {
            orderIntent.putExtra(OrderActivity.TYPE, OrderActivity.TYPE_ORDER_DONE);
            startActivity(orderIntent);
        });

        return view;
    }

    /**
     * 显示用户信息
     */
    void setUserInfoOnView() {
        userNameTextView.setText(userInfo.getNickName());
        phoneTextView.setVisibility(View.VISIBLE);
        phoneTextView.setText(userInfo.getLoginName());
        if (userInfo.getProfilePhotoBitmap() != null)
            userImageView.setImageBitmap(userInfo.getProfilePhotoBitmap());
        userInfoLayout.setOnClickListener((v) -> {
            Intent intent = new Intent(getActivity(), UserActivity.class);
            intent.putExtra("isLogin", true);
            startActivity(intent);
        });
    }

    /**
     * 隐藏用户信息
     */
    void hideUserInfoOnView() {
        userNameTextView.setText("登录/注册");
        phoneTextView.setVisibility(View.GONE);
        userImageView.setImageResource(R.drawable.wode);
        userInfoLayout.setOnClickListener((v) -> {
            Intent intent = new Intent(getActivity(), UserActivity.class);
            intent.putExtra("isLogin", false);
            startActivity(intent);
        });
    }

    public interface OnUserInfoListener {
        void getUserInfo(HjmObjectCallBack<UserInfo> callBack);
    }
}